//
//  GCMEquPlanViewController.m
//  GeeCityManager
//
//  Created by 聚城科技 on 16/5/25.
//  Copyright © 2016年 聚城科技. All rights reserved.
//

#import "DBManage.h"
#import "FMDatabase.h" 
#import "FMResultSet.h"

@implementation DBManage
{
    FMDatabase * _db;
}


+(id)defaultDBMessage
{
    static DBManage * manage = nil;
    if (manage == nil) {
        manage = [[DBManage alloc]init];
    }
    return manage;
}


-(id)init
{
    if ([super init]) {
        //在这个类被创建出来之前，要保证数据库被创建且正常打开
        //表单也要被成功创建出来
        [self createDB];
        [self createTable];
    }
    return self;
}


//创建并打开数据库
-(void)createDB
{
    if (_db == nil ) {
        NSString * path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/GeeCity"];
        _db = [FMDatabase databaseWithPath:path];
        
        BOOL ret = [_db open];
        if (ret == YES) {
            NSLog(@"数据库打开成功");
        }
    }
}


//创建表单
-(void)createTable
{
////////////////设备信息管理表/////////////////
    //1、写SQL
    NSString * sql = @"create table if not exists shebei (ID integer primary key autoincrement,TYPE text,SBID text,ZQ text,XJRQ text,DAYS text,SBNAME text);";
    
    //2、执行SQL
    BOOL ret = [_db executeUpdate:sql];
    if (ret == YES) {
        NSLog(@"表单创建成功");
    }
////////////////设备操作记录管理表/////////////////
    //1、写SQL
    NSString * operatesql = @"create table if not exists shebeioperate (ID integer primary key autoincrement,OPERATE text);";
    
    //2、执行SQL
    BOOL ret1 = [_db executeUpdate:operatesql];
    if (ret1 == YES) {
        NSLog(@"表单创建成功");
    }
}

- (void)deleteAllSheBei{
    
    NSString * sql = @"delete from shebei;";
    
    BOOL ret = [_db executeUpdate:sql];
    if (ret == YES) {
        NSLog(@"删除所有数据成功");
    }
}

- (void)addSheBei:(GCMOUTLINESheBei *)shebei
{
    //1、
    NSString * sql = @"insert into shebei (TYPE,SBID,ZQ,XJRQ,DAYS,SBNAME) values (?,?,?,?,?,?);";    
    //2、
    BOOL ret = [_db executeUpdate:sql,shebei.type,shebei.sbid,shebei.zq,shebei.xjrq,shebei.days,shebei.sbname];
    if (ret == YES) {
        NSLog(@"插入数据成功");
    }
}

- (void)changeWithSheBei:(GCMOUTLINESheBei*)shebei withID:(NSString*)oldID
{
    NSString * sql = @"update shebei set TYPE = ?,SBID=? ,ZQ=?, XJRQ=?, DAYS=?, SBNAME=? where ID = ?;";
    
    BOOL ret = [_db executeUpdate:sql,shebei.type,shebei.sbid,shebei.zq,shebei.xjrq,shebei.days,shebei.sbname,oldID];
    if (ret == YES) {
        NSLog(@"修改数据成功");
    }
}


-(NSArray*)allSheBei
{
    //创建一个数据
    NSMutableArray * array = [[NSMutableArray alloc]init];
    
    NSString * sql = @"select * from shebei;";
    FMResultSet * set = [_db executeQuery:sql];
    while ([set next]) {
        NSString * type = [set stringForColumn:@"TYPE"];
        NSString * sbid = [set stringForColumn:@"SBID"];
        NSString * zq = [set stringForColumn:@"ZQ"];
        NSString * xjrq = [set stringForColumn:@"XJRQ"];
        NSString * days = [set stringForColumn:@"DAYS"];
        NSString * sbname = [set stringForColumn:@"SBNAME"];
        NSString * idd = [set stringForColumn:@"ID"];
        
        GCMOUTLINESheBei * shebei = [[GCMOUTLINESheBei alloc]init];
        shebei.type = type;
        shebei.sbid = sbid;
        shebei.zq = zq;
        shebei.xjrq = xjrq;
        shebei.days = days;
        shebei.sbname = sbname;
        shebei.ID = idd;
        [array addObject:shebei];
    }
    
    return array;
}

- (void)deleteSheBeiWithID:(NSString*)ID
{
    NSString * sql = @"delete from user where ID=?;";
    
    BOOL ret = [_db executeUpdate:sql,ID];
    if (ret == YES) {
        NSLog(@"删除数据成功");
    }
}




/////////////////////设备操作界面///////////////////////////
- (void)deleteAlloperateSheBei{
    
    NSString * sql = @"delete from shebeioperate;";
    
    BOOL ret = [_db executeUpdate:sql];
    if (ret == YES) {
        NSLog(@"删除所有数据成功");
    }
}

- (void)addoperateSheBei:(GCMOUTLINECaoZuoModel *)shebei
{
    //1、
    NSString * sql = @"insert into shebeioperate (OPERATE) values (?);";
    
    //2、
    BOOL ret = [_db executeUpdate:sql,shebei.operate];
    if (ret == YES) {
        NSLog(@"插入数据成功");
    }
}

- (void)changeWithoperateSheBei:(GCMOUTLINECaoZuoModel*)shebei withID:(NSString*)oldID
{
    NSString * sql = @"update shebeioperate set OPERATE = ?;";
    
    BOOL ret = [_db executeUpdate:sql,shebei.operate];
    if (ret == YES) {
        NSLog(@"修改数据成功");
    }
}


-(NSArray*)alloperateSheBei
{
    //创建一个数据
    NSMutableArray * array = [[NSMutableArray alloc]init];
    
    NSString * sql = @"select * from shebeioperate;";
    FMResultSet * set = [_db executeQuery:sql];
    while ([set next]) {
        NSString * opetate = [set stringForColumn:@"OPERATE"];
        NSString * idd = [set stringForColumn:@"ID"];
        
        GCMOUTLINECaoZuoModel * shebei = [[GCMOUTLINECaoZuoModel alloc]init];
        shebei.operate = opetate;
        shebei.ID = idd;
        [array addObject:shebei];
        
    }
    
    return array;
}

- (void)deleteoperateSheBeiWithID:(NSString*)ID
{
    NSString * sql = @"delete from shebeioperate where ID=?;";
    
    BOOL ret = [_db executeUpdate:sql,ID];
    if (ret == YES) {
        NSLog(@"删除数据成功");
    }
}

@end







